<?php
namespace App\Service\ApiXcx;

use Illuminate\Support\Facades\DB;

class PostService extends BaseService
{

    /**
     * 列表
     * @param array $pages【start，limit】
     * @return array
     */
    public function getList($pages, $type, $uid)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbCmsPost)->where('del', 0)->where('status', 1);
        if ($type) {
            $db->where('type', $type);
        }
        if ($uid) {
            $db->where('uid', $uid);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('is_top', 'desc')
            ->orderBy('id', 'desc')
            ->get()
            ->map(function ($value){
                return (array)$value;
            })
            ->toArray();
        foreach ($res as &$item) {
            if ($item['cate_id']) {
                $cate_title = DB::table($this->dbCmsCategory)->where('id', $item['cate_id'])->value('cate_name');
            }
            $type_text_arr = [1 => '活动话题', 2 => '精选分享'];

            $item['type_text'] = !empty($type_text_arr[$item['type']]) ? $type_text_arr[$item['type']] : '活动话题';
            $item['cate_title'] = !empty($cate_title) ? $cate_title : '';

            $item['nickname'] = '';
            $item['avatarUrl'] = '';
            $user_info = DB::table($this->dbUser)->where('id', $item['uid'])->first();
            if ($user_info) {
                $item['nickname'] = $user_info->nickname;
                $item['avatarUrl'] = substr($user_info->avatarUrl, 0, 4)=='http' ?
                    $user_info->avatarUrl : env('DOMAIN') . '/'. $user_info->avatarUrl;
            }
            $item['image_list'] = [];
            $image_list = DB::table($this->dbCmsImg)
                ->where('post_id', $item['id'])
                ->where('del', 0)
                ->get()
                ->map(function ($value){
                    return (array)$value;
                })
                ->toArray();
            if($image_list){
                foreach ($image_list as $k => $v) {
                    $image_list[$k]['link'] = substr($v['link'], 0, 4)!='http' ?
                        env('DOMAIN') . '/' . $v['link'] : $v['link'];
                }
                $item['iamge_list'] = $image_list;
            }
        }
        if (!count($res)) {
            return array('code' => 0, 'msg' => '没有数据', 'data' => [], 'total' => 0);
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res, 'total' => $total);
    }

    /**
     * 详情
     * @param int $id
     * @return array
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbCmsPost)
            ->where('del', 0)
            ->where('status', 1)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => 0, 'msg' => '没有数据', 'data' => [], 'total' => 0);
        }
        if ($res['cate_id']) {
            $cate_title = $this->dbConn($this->dbCmsCategory)
                ->where('id', $res['cate_id'])
                ->value('cate_name');
        }
        $type_text_arr = [1 => '活动话题', 2 => '精选分享'];
        $res['type_text'] = !empty($type_text_arr[$res['type']]) ? $type_text_arr[$res['type']] : '活动话题';
        $res['cate_title'] = !empty($cate_title) ? $cate_title : '';
        $res['nickname'] = '';
        $res['avatarUrl'] = '';
        $user_info = $this->dbConn($this->dbUser)->where('id', $res['uid'])->first();
        if ($user_info) {
            $res['nickname'] = $user_info->nickname;
            $res['avatarUrl'] = substr($user_info->avatarUrl, 0, 4)=='http' ?
                $user_info->avatarUrl : env('DOMAIN') . '/'. $user_info->avatarUrl;
        }
        $res['image_list'] = [];
        $image_list = $this->dbConn($this->dbCmsImg)
            ->where('post_id', $res['id'])
            ->where('del', 0)
            ->get()
            ->map(function ($value){
                return (array)$value;
            })
            ->toArray();
        if($image_list){
            foreach ($image_list as $k => $v) {
                $image_list[$k]['link'] = substr($v['link'], 0, 4)!='http' ?
                    env('DOMAIN') . '/' . $v['link'] : $v['link'];
            }
            $res['iamge_list'] = $image_list;
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res);
    }

    /**
     * 详情
     * @param int $uid
     */
    public function getCateList()
    {
        $res = (array) DB::table($this->dbCmsCategory)->where('del', 0)
            ->get()
            ->map(function ($value){
                return (array)$value;
            })
            ->toArray();
        if (!count($res)) {
            return array('code' => 0, 'msg' => '数据不存在', 'data' => []);
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res, 'total' => count($res));
    }

    /*
     * 添加帖子
     * */
    public function add($post, $uid) {
        $data['type'] = $post['type'];
        $data['title'] = $post['title'];
        $data['content'] = $post['content'];
        $data['cate_id'] = $post['cate_id'];
        $data['uid'] = $uid;
        $data['addtime'] = $post['uptime'] = toDate();
        $post_id = DB::table($this->dbCmsPost)->insertGetId($data);
        if(!empty($post['images'])){
            //添加详情图
            foreach ($post['images'] as $v) {
                $img = file_exists($v['img']) ? env('DOMAIN').'/'.$v['img'] : $v['img'];
                $imgData = [
                    'post_id' => $post_id,
                    'link' => $img,
                    'width' => $v['w'],
                    'height' => $v['h'],
                    'addtime' => toDate(),
                ];
                $imgup = DB::table($this->dbCmsImg)->insertGetId($imgData);
                if (!$imgup) {
                    return array('code' => -2, 'msg' => 'error');
                }
            }
        }
        return array('code' => 0, 'msg' => 'success');
    }
}
